<?php
require_once dirname(__FILE__) . '/InterfaceBase.php';
require_once OPEN_LIB_PATH . '/open/models/OaServiceOPLogModel.php';
class PaymentInterface extends InterfaceBase implements IService
{
	# 0 普通卡和绿卡 1  仅普通卡  2 绿卡
	const BUSINESS_TYPE_ALL = 0;
	const BUSINESS_TYPE_COMMON = 1;
	const BUSINESS_TYPE_GREEN = 2;
	# 应用支付状态  0关闭 1待运营 2上线
	const PAYMENT_STATUS_CLOSE = 0;
	const PAYMENT_STATUS_TEST = 1;
	const PAYMENT_STATUS_ONLINE = 2;
	# 是否显示区组 0不显示，1显示区，2显示服
	const PAYMENT_SHOW_CLOSE = 0;
	const PAYMENT_SHOW_AREA = 1;
	const PAYMENT_SHOW_GROUP = 2;
	# 0 关闭充值，1 开通充值
	const PAYMENT_OPERATION_CLOSE = 0;
	const PAYMENT_OPERATION_OPEN = 1;
	# 同步操作0 同步所有 1仅同步应用状态 2仅同步授权用户 3 同步区组
	const PAYMENT_OPERATION_SYNC_ALL = 0;
	const PAYMENT_OPERATION_SYNC_STATUS = 1;
	const PAYMENT_OPERATION_SYNC_USER = 2;
	const PAYMENT_OPERATION_SYNC_AREA = 3;
	protected $serviceLogModel;

	public function __construct ($env = self::APP_ENV_TEST)
	{
		parent::__construct($env);
		$this->loadSdk("SDOServicePTB");
		$this->serviceLogModel = new OaServiceOPLogModel();
	}
	/**
	 * 开通支付服务
	 * @param array $message array("errNo"=>错误码, "errMsg"=>错误信息)
	 * @param array $args array(
	 * spId=>商户ID,
	 * appId=>应用ID,
	 * DepositConfigEx=>array(businessType=> 普通卡或绿卡, appStatus=> 应用状态)
	 * TransferConfigEx=>array(currencyName=>货币名称, currencyUnit=>货币单位, isLineRate=>是否线性,
	 * pointList=>IsLineRate=1时，表示比率, businessType => 普通卡或绿卡, showAreaServer=>是否显示区组, appStatus = 应用状态)
	 * )
	 * @param int $env 环境变量
	 * @return boolean
	 */
	public function open ($args, $env = null)
	{
		if (! $env) {
			$env = $this->env;
		}
		$this->startLog($args, $env, __FUNCTION__, __CLASS__);
		$msg = '';
		$sdkEnv = $this->getSDKEnvType($env);
		if (! isset($args['TransferConfigEx']) && ! isset($args['DepositConfigEx'])) {
			$this->setError(SDOServiceErrorCode::API_EC_PARAM, "Invalid Pamaters");
			$this->endLog(__CLASS__);
			return false;
		}
		/*$row = array('op_type'=>OaServiceOPLogModel::OP_TYPE_SERVICE_PAYMENT, 'status'=>OaServiceOPLogModel::OPERATION_UNDO,
		 'data'=>json_encode($args));
		 $operationId = $this->serviceLogModel->createServiceOPLog(0, $row);
		 if ($operationId===false) {
		 $this->setError(-20010001, 'create service oplog failure');
		 return false;
		 }*/
		$taskId = isset($args['task_id']) ? $args['task_id'] : 0;
		if (!isset($args['operationId'])) {
			$row = array('op_type'=>OaServiceOPLogModel::OP_TYPE_SERVICE_PAYMENT, 'status'=>OaServiceOPLogModel::OPERATION_UNDO,
            'data'=>json_encode($args), 'method_type'=>OaServiceOPLogModel::METHOD_TYPE_ADD, 'environment'=>$env);
			$operationId = $this->serviceLogModel->createServiceOPLog($taskId, $row);
			if ($operationId===false) {
				$this->setError(-20010001, 'create service oplog failure');
				$this->endLog(__CLASS__);
				return false;
			}
		} else {
			$operationId = $args['operationId'];
		}
		$spId = $args['spId'];
		$appId = $args['appId'];
		$beginDate = 0;
		$endDate = 0;
		$maxPoint = 15000;
		$minPoint = 1;
		if (isset($args['TransferConfigEx'])) {
			$this->processLog(__CLASS__, 'OpenPTBPayment');
			$appInfo = $args['TransferConfigEx'];
			$appInfo['spId'] = $spId;
			$appInfo['appId'] = $appId;
			$appInfo['operationId'] = $operationId;
			if (isset($appInfo['currencyName'])) {
				$appInfo['CurrencyName'] = $appInfo['currencyName'];
			}
			if (isset($appInfo['currencyUnit'])) {
				$appInfo['CurrencyUnit'] = $appInfo['currencyUnit'];
			}
			$appInfo['appTag'] = '';
			$appInfo['appIndex'] = '';
			if (!SDOServicePTB::OpenPTBPayment($msg, $appInfo, $sdkEnv)) {
				$this->setError(SDOServicePTB::getErrNo(), $msg);
				$this->endLog(__CLASS__);
				return false;
			}

		} else {
			$this->processLog(__CLASS__, 'OpenPTBDeposit');
			$appInfo = $args['DepositConfigEx'];
			$appInfo['spId'] = $spId;
			$appInfo['appId'] = $appId;
			$appInfo['appTag'] = '';
			$appInfo['appIndex'] = '';
			$appInfo['operationId'] = $operationId;
			if (!SDOServicePTB::OpenPTBDeposit($msg, $appInfo, $sdkEnv)) {
				$this->setError(SDOServicePTB::getErrNo(), $msg);
				$this->endLog(__CLASS__);
				return false;
			}
		}
		if (isset($appInfo['appStatus']) && $appInfo['appStatus']!=SDOServicePTB::APP_STATUS_TEST) {
			$row = array('op_type'=>OaServiceOPLogModel::OP_TYPE_SERVICE_PAYMENT, 'status'=>OaServiceOPLogModel::OPERATION_UNDO,
            'data'=>json_encode($args), 'method_type'=>OaServiceOPLogModel::METHOD_TYPE_UPDATE, 'environment'=>$env);
			$operationId = $this->serviceLogModel->createServiceOPLog($taskId, $row);
			if ($operationId===false) {
				$this->setError(-20010001, 'create service oplog failure');
				$this->endLog(__CLASS__);
				return false;
			}
			$appInfo['operationId'] = $operationId;
			$method = "";
			if ($appInfo['appStatus']==SDOServicePTB::APP_STATUS_ONLINE) {
				$method = 'OpenApp';
			} elseif($appInfo['appStatus']==SDOServicePTB::APP_STATUS_CLOSE) {
				$method = 'CloseApp';
			} elseif($appInfo['appStatus']==SDOServicePTB::APP_STATUS_TEST) {
				$method = 'DebugApp';
			} else {
				$this->setError(SDOServiceErrorCode::getErrNo(), "Invalid pamameter (AppStuts)");
				$this->endLog(__CLASS__);
				return false;
			}
			$this->processLog(__CLASS__, $method);
			if (! SDOServicePTB::$method($msg, $appInfo, $sdkEnv)) {
				$this->setError(SDOServicePTB::getErrNo(), $msg);
				$this->endLog(__CLASS__);
				return false;
			}
		}
		$this->endLog(__CLASS__);
		return true;
	}
	/**
	 * 关闭认证服务
	 * @param array $message array("errNo"=>错误码, "errMsg"=>错误信息)
	 * @param array $args array(
	 * spId=>商户ID,
	 * appId=>应用ID,
	 * )
	 * @param int $env 环境变量
	 * @return boolean
	 */
	public function close ($args, $env = null)
	{
		if (! $env) {
			$env = $this->env;
		}
		$this->startLog($args, $env, __FUNCTION__, __CLASS__);
		$msg = '';
		$sdkEnv = $this->getSDKEnvType($env);
		$taskId = isset($args['task_id']) ? $args['task_id'] : 0;
		if (!isset($args['operationId'])) {
			$row = array('op_type'=>OaServiceOPLogModel::OP_TYPE_SERVICE_PAYMENT, 'status'=>OaServiceOPLogModel::OPERATION_UNDO,
            'data'=>json_encode($args), 'method_type'=>OaServiceOPLogModel::METHOD_TYPE_CLOSE, 'environment'=>$env);
			$operationId = $this->serviceLogModel->createServiceOPLog($taskId, $row);
			if ($operationId===false) {
				$this->setError(-20010001, 'create service oplog failure');
				$this->endLog(__CLASS__);
				return false;
			}
		} else {
			$operationId = $args['operationId'];
		}
		$spId = $args['spId'];
		$appId = $args['appId'];

		$args['operationId'] = $operationId;
		if (! SDOServicePTB::CloseApp($msg, $args, $sdkEnv)) {
			$this->setError(SDOServicePTB::getErrNo(), $msg);
			$this->endLog(__CLASS__);
			return false;
		}
		$this->endLog(__CLASS__);
		return true;
	}
	/**
	 * 修改支付服务
	 * @param array $message array("errNo"=>错误码, "errMsg"=>错误信息)
	 * @param array $args array(
	 * spId=>商户ID,
	 * appId=>应用ID,
	 * DepositConfigEx=>array(businessType=> 普通卡或绿卡, appStatus=> 应用状态)
	 * TransferConfigEx=>array(currencyName=>货币名称, currencyUnit=>货币单位, isLineRate=>是否线性,
	 * pointList=>IsLineRate=1时，表示比率, businessType => 普通卡或绿卡, showAreaServer=>是否显示区组, appStatus = 应用状态),
	 * appStatus=>array('status'=>应用状态),
	 * )
	 * @param int $env 环境变量
	 * @return boolean
	 */
	public function update ($args, $env = null)
	{
		if (! $env) {
			$env = $this->env;
		}
		$this->startLog($args, $env, __FUNCTION__, __CLASS__);
		$msg = '';
		$taskId = isset($args['task_id']) ? $args['task_id'] : 0;
		/*if (SDOServiceAuthen::searchApp($msg, $args['appId'], $this->getSDKEnvType($env))) {
		 $message = $this->getErrMsg(-20003001, "APP EXISTS");
		 return true;
		 }*/
		if (!isset($args['operationId'])) {
			$row = array('op_type'=>OaServiceOPLogModel::OP_TYPE_SERVICE_PAYMENT, 'status'=>OaServiceOPLogModel::OPERATION_UNDO,
            'data'=>json_encode($args), 'method_type'=>OaServiceOPLogModel::METHOD_TYPE_UPDATE, 'environment'=>$env);
			$operationId = $this->serviceLogModel->createServiceOPLog($taskId, $row);
			if ($operationId===false) {
				$this->setError(-20010001, 'create service oplog failure');
				$this->endLog(__CLASS__);
				return false;
			}
		} else {
			$operationId = $args['operationId'];
		}
		$spId = $args['spId'];
		$appId = $args['appId'];
		$sdkEnv = $this->getSDKEnvType($env);
		$beginDate = 0;
		$endDate = 0;
		$maxPoint = 15000;
		$minPoint = 1;
		if (isset($args['TransferConfigEx'])) {
			$this->processLog(__CLASS__, 'UpdateAppPaymentInfo');
			$appInfo = $args['TransferConfigEx'];
			$appInfo['spId'] = $spId;
			$appInfo['appId'] = $appId;
			if (isset($appInfo['currencyName'])) {
				$appInfo['CurrencyName'] = $appInfo['currencyName'];
			}
			if (isset($appInfo['currencyUnit'])) {
				$appInfo['CurrencyUnit'] = $appInfo['currencyUnit'];
			}
			$appInfo['operationId'] = $operationId;
			//            $appInfo['appTag'] = '';
			//            $appInfo['appIndex'] = '';
			if (!SDOServicePTB::UpdateAppPaymentInfo($msg, $appInfo, $sdkEnv)) {
				$this->setError(SDOServicePTB::getErrNo(), $msg);
				$this->endLog(__CLASS__);
				return false;
			}
		} elseif (isset($args['DepositConfigEx'])) {
			$this->processLog(__CLASS__, 'OpenPTBDeposit');
			$appInfo = $args['DepositConfigEx'];
			$appInfo['spId'] = $spId;
			$appInfo['appId'] = $appId;
			$appInfo['appTag'] = '';
			$appInfo['appIndex'] = '';
			$appInfo['operationId'] = $operationId;
			if (!SDOServicePTB::OpenPTBDeposit($msg, $appInfo, $sdkEnv)) {
				$this->setError(SDOServicePTB::getErrNo(), $msg);
				$this->endLog(__CLASS__);
				return false;
			}
			if (isset($appInfo['appStatus']) && $appInfo['appStatus']!=SDOServicePTB::APP_STATUS_TEST) {
				$method = "";
				if ($appInfo['appStatus']==SDOServicePTB::APP_STATUS_ONLINE) {
					$method = 'OpenApp';
				} elseif($appInfo['appStatus']==SDOServicePTB::APP_STATUS_CLOSE) {
					$method = 'CloseApp';
				} else {
					$this->setError(SDOServiceErrorCode::getErrNo(), "Invalid pamameter (AppStuts)");
					$this->endLog(__CLASS__);
					return false;
				}
				$this->processLog(__CLASS__, $method);
				if (! SDOServicePTB::$method($msg, $appInfo, $sdkEnv)) {
					$this->setError(SDOServicePTB::getErrNo(), $msg);
					$this->endLog(__CLASS__);
					return false;
				}
			}
		}
		if (isset($args['appStatus'])) {
			$appStatus = $args['appStatus'];
			$appStatus['spId'] = $spId;
			$appStatus['appId'] = $appId;
			$appStatus['operationId'] = $operationId;
			$method = "";
			if ($appStatus['status']==SDOServicePTB::APP_STATUS_ONLINE) {
				$method = 'OpenApp';
			} elseif($appStatus['status']==SDOServicePTB::APP_STATUS_CLOSE) {
				$method = 'CloseApp';
			} elseif($appStatus['status']==SDOServicePTB::APP_STATUS_TEST) {
				$method = 'DebugApp';
			} else {
				$this->setError(SDOServiceErrorCode::getErrNo(), "Invalid pamameter (AppStuts)");
				$this->endLog(__CLASS__);
				return false;
			}
			$this->processLog(__CLASS__, $method);
			if (! SDOServicePTB::$method($msg, $appStatus, $sdkEnv)) {
				$this->setError(SDOServicePTB::getErrNo(), $msg);
				$this->endLog(__CLASS__);
				return false;
			}
		}
		$this->endLog(__CLASS__);
		return true;
	}
	/**
	 * 获取支付信息
	 * @param array $message
	 * @param array $args array(
	 * 'spId'=>商户ID
	 * 'appId'=>应用ID,
	 * )
	 * @param mixed $env
	 * @return boolean|array
	 */
	public function get ($args, $env = null)
	{
		if (! $env) {
			$env = $this->env;
		}
		$this->startLog($args, $env, __FUNCTION__, __CLASS__);
		$msg = '';
		$sdkEnv = $this->getSDKEnvType($env);
		/*if (!isset($args['operationId'])) {
		 $row = array('op_type'=>OaServiceOPLogModel::OP_TYPE_SERVICE_PAYMENT, 'status'=>OaServiceOPLogModel::OPERATION_SUCCESS,
		 'data'=>json_encode($args), 'method_type'=>OaServiceOPLogModel::METHOD_TYPE_UPDATE, 'environment'=>$env);
		 $operationId = $this->serviceLogModel->createServiceOPLog(0, $row);
		 if ($operationId===false) {
		 $this->setError(-20010001, 'create service oplog failure');
		 return false;
		 }
		 } else {
		 $operationId = $args['operationId'];
		 }*/
		$appId = $args['appId'];
		$spId = $args['spId'];
		$args['operationId'] = '123';
		$result = SDOServicePTB::GetAppPaymentInfo($msg, $args, $sdkEnv);
		if ($result===false) {
			$this->setError(SDOServicePTB::getErrNo(), $msg);
			$this->endLog(__CLASS__);
			$this->serviceLogModel->setServiceOPStatus(OaServiceOPLogModel::OPERATION_FAILURE, $operationId);
		}
		$this->endLog(__CLASS__);
		return $result;
	}
	/**
	 *
	 * @param array $message
	 * @param array $args array(
	 * 'spId'=>商户ID
	 * 'appId'=>应用ID,
	 * 'testAccounts'=>array(
	 * 测试账号, 'kaka'....
	 * ),
	 * 'syncArea'=>1 (是否同步区组)
	 * 'syncApp'=>1 (是否同步应用信息)
	 * )
	 * @param mixed $env
	 */
	public function sync ($args, $env = null)
	{
		if (! $env) {
			$env = $this->env;
		}
		$this->startLog($args, $env, __FUNCTION__, __CLASS__);
		$msg = '';
		$sdkEnv = $this->getSDKEnvType($env);
		$taskId = isset($args['task_id']) ? $args['task_id'] : 0;
		if (!isset($args['operationId'])) {
			$row = array('op_type'=>OaServiceOPLogModel::OP_TYPE_SERVICE_PAYMENT, 'status'=>OaServiceOPLogModel::OPERATION_UNDO,
            'data'=>json_encode($args), 'method_type'=>OaServiceOPLogModel::METHOD_TYPE_SYNC, 'environment'=>$env);
			$operationId = $this->serviceLogModel->createServiceOPLog($taskId, $row);
			if ($operationId===false) {
				$this->setError(-20010001, 'create service oplog failure');
				$this->endLog(__CLASS__);
				return false;
			}
		} else {
			$operationId = $args['operationId'];
		}
		$appId = $args['appId'];
		$spId = $args['spId'];
		$appStatus = self::PAYMENT_STATUS_TEST;
		$isSleep = 0;
		if (isset($args['testAccounts'])) {
			$this->processLog(__CLASS__, 'AuthorizeUsers');
			$testAccounts = $args['testAccounts'];
			$appInfo = array('spId'=>$spId, 'appId'=>$appId, 'operationId'=>$operationId, 'authorizedUsers'=>$testAccounts);
			if (! SDOServicePTB::AuthorizeUsers($msg, $appInfo, $sdkEnv)) {
				$this->setError(SDOServicePTB::getErrNo(), $msg);
				$this->endLog(__CLASS__);
				return false;
			}
			$isSleep = 1;
		}
		if (isset($args['syncArea']) && $args['syncArea']) {
			if ($isSleep) {
				sleep(4);
			}
			$this->processLog(__CLASS__, 'syncArea');
			$appInfo = array('spId'=>$spId, 'appId'=>$appId, 'operationId'=>$operationId, 'operationType'=>1);
			$appInfo['appTag'] = '';
			$appInfo['appIndex'] = '';
			if (! SDOServicePTB::UpdateAppInfo($msg, $appInfo, $sdkEnv)) {
				$this->setError(SDOServicePTB::getErrNo(), $msg);
				$this->endLog(__CLASS__);
				return false;
			}
		}
		if (isset($args['syncApp']) && $args['syncApp']) {
			if ($isSleep) {
				sleep(4);
			}
			$this->processLog(__CLASS__, 'syncApp');
			$appInfo = array('spId'=>$spId, 'appId'=>$appId, 'operationId'=>$operationId, 'operationType'=>2);
			$appInfo['appTag'] = '';
			$appInfo['appIndex'] = '';
			if (! SDOServicePTB::UpdateAppInfo($msg, $appInfo, $sdkEnv)) {
				$this->setError(SDOServicePTB::getErrNo(), $msg);
				$this->endLog(__CLASS__);
				return false;
			}
		}
		$this->endLog(__CLASS__);
		return true;
	}
}